package me.jessyan.mvparms.demo.mvp.presenter;


import android.app.Application;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.mvparms.demo.app.utils.RxUtils;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.entity.BaseJson;
import me.jessyan.mvparms.demo.mvp.model.entity.ParamJson;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;

@ActivityScope
public class UserPresenter extends BasePresenter<UserContract.Model, UserContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private AppManager mAppManager;

    private int Retry = 3;
    private int Delay = 1;

    @Inject
    public UserPresenter(UserContract.Model model, UserContract.View rootView
            , RxErrorHandler handler, Application application
            , AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }

    // 验证码登录
    public void UserLoginByMessage(String phone,String code) {
//        mRootView.showLoading();
//        ParamJson param = new ParamJson();
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("phone",phone);
//        result.put("code",code);
//        param.setData(result);
//        mModel.LoginByCode(param)
//                .subscribeOn(Schedulers.io())
//                .retryWhen(new RetryWithDelay(Retry, Delay))
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtils.<BaseJson<User>>bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseJson<User>>(mErrorHandler) {
//
//                    @Override
//                    public void onNext(@NonNull BaseJson<User> bean) {
//                        if(bean.isSuccess()){
//
//                        }else{
//
//                        }
//                        mRootView.hideLoading();
//                    }
//                });
    }

    // 登录
    public void UserLogin(String phone, String password) {
//        mRootView.showLoading();
//        ParamJson param = new ParamJson();
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("phone",phone);
//        result.put("password",password);
//        param.setData(result);
//        mModel.Login(param)
//                .subscribeOn(Schedulers.io())
//                .retryWhen(new RetryWithDelay(Retry, Delay))
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtils.<BaseJson<User>>bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseJson<User>>(mErrorHandler) {
//
//                    @Override
//                    public void onNext(@NonNull BaseJson<User> bean) {
//                        if(bean.isSuccess()){
//
//                        }else{
//                            mRootView.hideLoading();
//                        }
//
//                    }
//                });
    }

    //注册用户
    public void RegisterUser(String phone, String password) {
//        mRootView.showLoading();
//        ParamJson param = new ParamJson();
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("phone",phone);
//        result.put("password",password);
//        param.setData(result);
//        mModel.RegisterUser(param)
////                .subscribeOn(Schedulers.io())
//                .retryWhen(new RetryWithDelay(Retry, Delay))
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtils.<BaseJson<User>>bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseJson<User>>(mErrorHandler) {
//
//                    @Override
//                    public void onNext(@NonNull BaseJson<User> bean) {
//                        if(bean.isSuccess()){
//
//                        }else{
//                            mRootView.hideLoading();
//                        }
//                    }
//                });
    }

    //发送短信
    public void SendMessage(String phone,int type) {
        //type  0注册1登录2修改密码
//        mRootView.showLoading();
//        ParamJson param = new ParamJson();
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("phone",phone);
//        result.put("type",type);
//        param.setData(result);
//        mModel.sendSMS(param)
//                .subscribeOn(Schedulers.io())
//                .retryWhen(new RetryWithDelay(Retry, Delay))
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtils.<BaseJson<String>>bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseJson<String>>(mErrorHandler) {
//
//                    @Override
//                    public void onNext(@NonNull BaseJson<String> bean) {
//                        if(bean.isSuccess()){
//                            Intent intent = new Intent();
//                            mRootView.launchActivity(intent);
//                            mRootView.showMessage("发送成功");
//                        }else {
//
//                        }
//                        mRootView.hideLoading();
//                    }
//                });
    }

    //检查短信
    public void CheckSMSCode(String phone,String code,int type) {
        //type  0注册1登录2修改密码
//        mRootView.showLoading();
//        ParamJson param = new ParamJson();
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("phone",phone);
//        result.put("type",type);
//        result.put("code",code);
//        param.setData(result);
//        mModel.checkSMS(param)
//                .subscribeOn(Schedulers.io())
//                .retryWhen(new RetryWithDelay(Retry, Delay))
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtils.<BaseJson<String>>bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseJson<String>>(mErrorHandler) {
//
//                    @Override
//                    public void onNext(@NonNull BaseJson<String> bean) {
//                        if(bean.isSuccess()){
//                            Intent intent = new Intent();
//                            mRootView.launchActivity(intent);
//
//                        }else{
//                        }
//                        mRootView.hideLoading();
//                    }
//                });
    }


//    public void getInvestorAuth(String token,View mContent,UserPresenter mPresenter){
//        mRootView.showLoading();
//        ParamJson param = new ParamJson();
//        param.setToken(token);
//        mModel.getInvestorAuth(param)
//                .subscribeOn(Schedulers.io())
//                .retryWhen(new RetryWithDelay(Retry, Delay))
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtils.<BaseJson<Integer>>bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseJson<Integer>>(mErrorHandler) {
//
//                               @Override
//                               public void onNext(@NonNull BaseJson<Integer> result) {
//                                   if(result.isSuccess()) {
//                                       if(result.getData() == 0){
////                                           AllDialogUtil.getInstance().showAuthHintDialog(mAppManager.getCurrentActivity(),mContent,token,mPresenter);
//                                       }else {
//                                           ArmsUtils.startActivity(MainActivity.class);
//                                           mAppManager.getCurrentActivity().finish();
//                                       }
//                                   }else{
//                                       mRootView.showMessage(result.getMessage());
//                                   }
//                                   mRootView.hideLoading();
//                               }
//
//                               @Override
//                               public void onError(@NonNull Throwable e) {
//                                   super.onError(e);
//                                   ArmsUtils.startActivity(UserMessageLoginActivity.class);
//                                   mAppManager.getCurrentActivity().finish();
//                               }
//                           }
//                );
//    }


}